<?php

namespace CMS\Service;

/**
 * 模型字段属性相关处理
 */
class Attribute
{
    /**
     *
     * @var array 
     */
    private static $list;
    
    /**
     *
     * @var string 
     */
    private static $cacheKey = 'MODEL_ATTRIBUTE_LIST';

    /**
     * 获取属性信息
     * 
     * @param integer $id 属性ID
     * @param string $field 要获取的字段名
     * @return array 属性信息
     */
    public static function all($model_id, $group = true, $fields = true)
    {

        /* 非法ID */
        if (empty($model_id) || !is_numeric($model_id)) {
            return '';
        }

        /* 获取属性 */
        if (!isset(static::$list[$model_id])) {
            $map = array(
                'model_id' => $model_id
            );
            $extend = M('Model')->getFieldById($model_id, 'extend');

            if ($extend) {
                $map = array(
                    'model_id' => array(
                        "in",
                        array(
                            $model_id,
                            $extend
                        )
                    )
                );
            }
            $info = M('Attribute')->where($map)
                    ->field($fields)
                    ->select();
            static::$list[$model_id] = $info;
        }

        $attr = array();
        if ($group) {
            foreach (static::$list[$model_id] as $value) {
                $attr[$value['id']] = $value;
            }
            $model = M('Model')->field('field_sort,attribute_list,attribute_alias')->find($model_id);
            $attribute = explode(',', $model['attribute_list']);
            if (empty($model['field_sort'])) { // 未排序
                $group = array(
                    1 => array_merge($attr)
                );
            } else {
                $group = json_decode($model['field_sort'], true);

                $keys = array_keys($group);
                foreach ($group as &$value) {
                    foreach ($value as $key => $val) {
                        $value[$key] = $attr[$val];
                        unset($attr[$val]);
                    }
                }

                if (!empty($attr)) {
                    foreach ($attr as $key => $val) {
                        if (!in_array($val['id'], $attribute)) {
                            unset($attr[$key]);
                        }
                    }
                    $group[$keys[0]] = array_merge($group[$keys[0]], $attr);
                }
            }
            if (!empty($model['attribute_alias'])) {
                $alias = preg_split('/[;\r\n]+/s', $model['attribute_alias']);
                $fields = array();
                foreach ($alias as &$value) {
                    $val = explode(':', $value);
                    $fields[$val[0]] = $val[1];
                }
                foreach ($group as &$value) {
                    foreach ($value as $key => $val) {
                        if (!empty($fields[$val['name']])) {
                            $value[$key]['title'] = $fields[$val['name']];
                        }
                    }
                }
            }
            $attr = $group;
        } else {
            foreach (static::$list[$model_id] as $value) {
                $attr[$value['name']] = $value;
            }
        }
        
        return $attr;
    }

}
